package cn.wizzer.framework.util;

import cn.wizzer.framework.base.Result;
import cn.wizzer.framework.base.exceptions.BizException;

import java.util.Arrays;
import java.util.Collection;
import java.util.Map;

public class ValidateUtil {
    public static void check(Map<String,Object> map) {
        map.forEach((key,value) -> {
            if (ValidateUtil.isEmpty(value)) {
                throw new BizException(key + " can not be null!");
            }
        });
    }

    public static Boolean isEmpty(Object object) {
        if (null == object) {
            return true;
        }
        if (object.getClass().equals(String.class)) {
            return object.equals("") || object.equals("null");
        }
        if (object instanceof Collection) {
            return ((Collection) object).size() <= 0;
        }
        if (object instanceof Map) {
            return ((Map) object).keySet().isEmpty();
        }
        if (object instanceof Object[]) {
            return ((Object[]) object).length <= 0;
        }
        return false;
    }

    public static void main(String[] args){
        //amount:10 appid:1213 phone:null
        ValidateUtil.check(
                Result.createMap(
                        "a","1",
                        "b","",
                        "c","")
        );
    }
}
